from querier.esquerier import ElasticSearchQuerier


class JDShopGoodsMatchAllQuerier(ElasticSearchQuerier):
    def __init__(self, shop_match_querier, shop_goods_querier):
        super(JDShopGoodsMatchAllQuerier, self).__init__(None, None, None)
        self.shop_querier = shop_match_querier
        self.shop_goods_querier = shop_goods_querier

    def search(self, args):
        shop = self.shop_querier.search(args)
        shop_id = [str(t['shop_id']) for t in shop['shop']]
        total = shop['total']
        keywords = shop['ex_keywords']
        shops_infos = shop['shop']

        res = {}
        for value in shop_id:
            a = {"shop_id": value}
            res[value] = self.shop_goods_querier.search(a)['goods']

        return {
            "shop_id": [t[0:10] for t in shop_id],
            'total': total,
            'keywords': keywords,
            'shops_info': shops_infos,
            "goods_info": res

        }

    def _build_query(self, args): pass

    def _build_result(self, es_result, param): pass
